package net.asg.games.yokeltowers.view;

import net.asg.games.yokeltowers.images.ImageProvider;
import net.asg.games.yokeltowers.sounds.SoundManager;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;


/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/


/**
 * @author Blakbro2k This class defines the moving object on the screen of the
 *         game.
 * 
 *         It requires an image provider to handle the textures. It returns an
 *         array of texture regions if the object has animations
 */
public class YokelGameObject {
	public enum ObjectState { FALLING, SET, SETTING, TOCHEDDOWN }

	protected int moveSpeed = 16;

	protected float animationPeriod = 0.10f;

	protected int width;

	protected int height;

	public Rectangle rect;

	protected int frame = 0;

	protected float time = 0;

	protected boolean isHitboxActive;

	protected TextureRegion[] textureRegions;

	protected ImageProvider imageProvider;
	
	protected SoundManager soundManager;
	
	public boolean isCollided = false;

	protected Rectangle getPosition() {
		return rect;
	}	

	public YokelGameObject(ImageProvider imageProvider,
			TextureRegion[] textureRegions, SoundManager soundManager, int width, int height, boolean isHitboxActive) {
		this.imageProvider = imageProvider;
		this.soundManager = soundManager;
		this.textureRegions = textureRegions;
		this.isHitboxActive = isHitboxActive;
		this.height = height;
		this.width = width;
		
		rect = new Rectangle();
		rect.width = width;
		rect.height = height;
		
		this.rect.x = this.imageProvider.getScreenWidth() / 2;
		this.rect.y = this.imageProvider.getScreenHeight();
		
	}

	public void moveLeft(float delta, float speedBonus, Rectangle bounds) {
		rect.x -= moveSpeed * delta * speedBonus;
		// state.setPosY((int) rect.y);
		time += delta;
		if (time > animationPeriod) {
			time -= animationPeriod;
			frame += 1;
			if (frame >= textureRegions.length) {
				frame = 0;
			}
		}
		
		keepOnScreen(bounds);
	}
	
	public void moveDown(float delta, float speedBonus, Rectangle bounds) {
		rect.y -= moveSpeed * delta * speedBonus;
		// state.setPosY((int) rect.y);
		time += delta;
		if (time > animationPeriod) {
			time -= animationPeriod;
			frame += 1;
			if (frame >= textureRegions.length) {
				frame = 0;
			}
		}
		
		keepOnScreen(bounds);
	}
	
	public void moveRight(float delta, float speedBonus, Rectangle bounds) {
		rect.x += moveSpeed * delta * speedBonus;
		// state.setPosY((int) rect.y);
		time += delta;
		if (time > animationPeriod) {
			time -= animationPeriod;
			frame += 1;
			if (frame >= textureRegions.length) {
				frame = 0;
			}
		}
		
		keepOnScreen(bounds);
		}

	public void draw(SpriteBatch batch) {
		batch.draw(textureRegions[frame], rect.x, rect.y);
	}
	
	public void drawDebug(ShapeRenderer debugRenderer){
        debugRenderer.rect(rect.x, rect.y, rect.width, rect.height);
	}

	public boolean isOverlapping(Rectangle otherRect) {
		return rect.overlaps(otherRect);
	}

	public boolean isHitboxActive() {
		return isHitboxActive;
	}

	public void setHitboxActive(boolean bool) {
		isHitboxActive = bool;
	}
	
	public void setMoveSpeed(int moveSpeed){
		this.moveSpeed = moveSpeed;
	}
	
	public void setAnimationSpeed(float f){
		this.animationPeriod = f;
	}
	
	public String doCollision(float delta){
		return null;
	}

	public boolean isBottomOfScreen() {
		return rect.y < 0;
	}
	
	public void setX(int x){
		rect.setX(x);
	}
	
	public void setY(int y){
		rect.setY(y);
	}
	
    protected void keepOnScreen(Rectangle bounds) {
        if (rect.x < bounds.x) {
        	rect.x = bounds.x;
        }
        else if (rect.x + width > bounds.width + bounds.x) {
        	rect.x = (bounds.width + bounds.x ) - width;
        }
    }
}
